package com.ykdl.member.kid.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import com.ykdl.member.kid.R;
import com.ykdl.member.kid.image.ImageLoader;
import com.ykdl.member.kid.image.ImageOptions;
import com.ykdl.member.kid.image.ImageProcessor;
import com.ykdl.member.kid.image.ImageUtil;
import com.ykdl.member.kid.image.ImageLoader.ImageLoaderListener;
/**
 * 加载网络图片的控件
 * @see
 * @since 1.0
 */
public class AsyncImageView extends ImageView {
	public static final String TAG = "AsyncImageView";

	public static final int URLTYPE_NETWORK = 1;
	public static final int URLTYPE_LOCAL = 2;

	public static final int DST_SRC = 1;
	public static final int DST_BACKGROUND = 2;

	private static long mMainThreadId = -1;
	private String mUrl;
	private Drawable mDefaultDrawable;
	private Bitmap mImage;
	private String mActivity = ImageLoader.GLOBAL_ACTIVITY;
	private Handler mHandler;
	private ImageProcessor mImageProcessor;
	private int mUrlType = URLTYPE_NETWORK;
	private int mDst = DST_BACKGROUND;
	private boolean mLoaded = false;

	static {
		mMainThreadId = Thread.currentThread().getId();
	}

	public AsyncImageView(Context context) {
		super(context);
		mHandler = new Handler();
	}

	public AsyncImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray array = context.obtainStyledAttributes(attrs,
				R.styleable.AsyncImageView);
		Drawable drawable = array
				.getDrawable(R.styleable.AsyncImageView_defaultDrawable);

		mDst = array.getInt(R.styleable.AsyncImageView_dst, DST_SRC);
		setDefaultDrawable(drawable);
		mUrlType = array.getInt(R.styleable.AsyncImageView_urlType,
				URLTYPE_NETWORK);
		mHandler = new Handler();
	}

	public boolean isLoaded() {
		return mLoaded;
	}

	public void setUrlType(int urlType) {
		if (mUrlType == urlType) {
			return;
		}
		mUrlType = urlType;
	}

	public void setDst(int dst) {
		if (mDst == dst) {
			return;
		}
		mDst = dst;
	}

	public ImageProcessor getImageProcessor() {
		return mImageProcessor;
	}

	public void setImageProcessor(ImageProcessor imageProcessor) {
		mImageProcessor = imageProcessor;
	}

	protected Bitmap processImage(Bitmap image) {
		if (mImageProcessor == null) {
			return image;
		}

		try {
			Bitmap newImage = mImageProcessor.process(image);
			return newImage;
		} catch (Throwable e) {
		}
		return image;
	}

	protected void loadImageCompleted(Bitmap image) {
		Bitmap processedImage = processImage(image);
		setImage(processedImage);
		recycle();
		mImage = processedImage;
	}

	private void _onSuccess(String activity, String url, Bitmap image,
			Object tag) {
		if (url.equals(mUrl)) {
			Log.d(TAG, "end get image:" + url+"activity:"+activity);
			loadImageCompleted(image);
		}
	}

	private ImageLoaderListener mImageLoaderListener = new ImageLoaderListener() {
		@Override
		public void onSuccess(final String activity, final String url,
				final Bitmap image, final Object tag) {
			if (Thread.currentThread().getId() == mMainThreadId) {
				_onSuccess(activity, url, image, tag);
				return;
			}
			mHandler.post(new Runnable() {
				@Override
				public void run() {
					_onSuccess(activity, url, image, tag);
				}
			});
		}

		@Override
		public void onError(String activity, String url, Object tag) {

		}

		@Override
		public void onCancel(String activity, String url, Object tag) {

		}
	};

	public Bitmap getImage() {
		return mImage;
	}

	private void setImage(Bitmap image) {
		mLoaded = true;
		if (mDst == DST_SRC) {
			super.setImageBitmap(image);
		} else if (mDst == DST_BACKGROUND) {
			super.setBackgroundDrawable(image != null ? new BitmapDrawable(
					image) : null);
		}
	}

	private void setDefaultDrawable() {
		if (mDst == DST_SRC) {
			super.setImageDrawable(mDefaultDrawable);
		} else if (mDst == DST_BACKGROUND) {
			super.setBackgroundDrawable(mDefaultDrawable);
		}
	}

	private void asyncLoadImage(ImageOptions options) {
		if (mActivity == null || mUrl == null) {
			return;
		}
		if (mUrl.equals("")) {
			return;
		}
		Log.d(TAG, "start get image:" + mUrl);
		if (mUrlType == URLTYPE_NETWORK) {
			ImageLoader.getInstance().asyncLoadImage(mActivity, mUrl,
					mImageLoaderListener, options);
		} else {
			Bitmap image = ImageUtil.decodeFile(mUrl, options,
					"AsyncImageView:" + mUrl);
			if (image != null) {
				loadImageCompleted(image);
			}
		}
	}

	public String getActivity() {
		return mActivity;
	}

	public void setUrl(String activity, String url,ImageOptions options) {
		if (TextUtils.equals(mUrl, url)) {
			return;
		}
		mActivity = activity;
		mUrl = url;
		setImage(null);
		recycle();
		setDefaultDrawable();
		asyncLoadImage(options);
	}

	public String getUrl() {
		return mUrl;
	}

	public int getUrlType() {
		return mUrlType;
	}

	public Drawable getDefaultBitmapDrawable() {
		return mDefaultDrawable;
	}

	public void setDefaultDrawable(Drawable drawable) {
		if (mDefaultDrawable != drawable) {
			mDefaultDrawable = drawable;
			if (mImage == null) {
				setDefaultDrawable();
			}
		}
	}

	@Override
	public void setBackgroundColor(int color) {
		super.setBackgroundColor(color);
		if (mDst == DST_BACKGROUND) {
			recycle();
		}
	}

	@Override
	public void setBackgroundDrawable(Drawable d) {
		super.setBackgroundDrawable(d);
		if (mDst == DST_BACKGROUND) {
			recycle();
		}
	}

	@Override
	public void setBackgroundResource(int resid) {
		super.setBackgroundResource(resid);
		if (mDst == DST_BACKGROUND) {
			recycle();
		}
	}

	@Override
	public void setImageBitmap(Bitmap bm) {
		super.setImageBitmap(bm);
		if (mDst == DST_SRC) {
			recycle();
		}
	}

	@Override
	public void setImageDrawable(Drawable drawable) {
		super.setImageDrawable(drawable);
		if (mDst == DST_SRC) {
			recycle();
		}
	}

	@Override
	public void setImageResource(int resId) {
		super.setImageResource(resId);
		if (mDst == DST_SRC) {
			recycle();
		}
	}

	private void recycle() {
		if (mImage != null) {
			mImage = null;
			mLoaded = false;
		}
	}
}
